<?php
/**
 * 处理警员的添加、修改、删除，根据传递过来的act判断相应的操作
 */
session_start();
if (!isset ($_SESSION["id"])) {
	session_destroy();
	echo "<script language='javascript'>top.location.href = 'Login.php';</script>";
	exit;
}
if(isset($_POST['act'])||isset($_GET['act'])){
	include_once 'public/DB_Oracle.php';
	include_once 'public/Json.php';
	$DBsql = new DB_Oracle ();
	$act=(!empty($_POST['act']))?$_POST['act']:$_GET['act'];
	
	if ($act=='chuli') {//查询某警员相关信息，用于更新警员信息
		$sn=$_POST['sn'];
		$sn=rtrim($sn,',');
		$sql="update gps_car_use_approval g set g.SQ_STATUS=1 where g.id in ($sn)";
		//exit($sql);
		$row=$DBsql->query($sql);
		echo 'y';
	}else if ($act=='select') {//查询某警员相关信息，用于更新警员信息
		$sn=$_POST['sn'];
		$sql="SELECT id,   
       en_id,   
       out_car_numb,   
       user_id,   
       pilotid,   
       to_char(sq_time,'YYYY-MM-DD HH24:MI:SS') as sq_time,   
       to_char(sy_time,'YYYY-MM-DD HH24:MI:SS') as sy_time,   
       to_char(gh_time,'YYYY-MM-DD HH24:MI:SS') as gh_time,   
       car_id,   
       address,   
       subject_matter,   
       sq_status,   
       path,   
       user_name    
  FROM gps_car_use_approval   
 WHERE  id = ".$sn."";

		$row=$DBsql->fetch_one_array($sql);
	//	exit($row);
		echo JSON($row);
	}else if($act=='up'){
		$sn=$_POST['sn'];
		$qiye=$_POST['EN_ID'];
		$chuche=$_POST['OUT_CAR_NUMB'];
		$chepai=$_POST['CAR_ID'];
		$mudi=$_POST['ADDRESS'];
		$shiyou=$_POST['SUBJECT_MATTER']; 
		$shenqingren=$_POST['USER_ID']; 
		$guihuan="to_date('".$_POST['GH_TIME']."', 'yyyy-mm-dd hh24:mi:ss')";
		$sql="update  gps_car_use_approval
		set
	   EN_ID='$qiye',   
       OUT_CAR_NUMB='$chuche',   
       USER_ID='$shenqingren',   
       GH_TIME=$guihuan,   
       CAR_ID='$chepai',   
       ADDRESS='$mudi',   
       SUBJECT_MATTER='$shiyou'
	   where id= '$sn'";

		if($DBsql->query($sql)){
			echo '{
				"info":"数据更新成功！",
				"status":"y"
			}';
		}else{
			echo '{
				"info":"数据更新失败！",
				"status":"n"
			}';
		}
	}else if($act=='valid'){//新增警员，先验证警号是否可用
		$number=$_POST['param'];//警号
		$sql="select dp.personnel_index,
			       dp.personnel_name,
			       dp.personnel_gender,
			       dp.personnel_sn,
			       dd.department_name,
       			   dd.department_index
			  from dep_personnel dp, dep_department dd
			 where dp.department_index = dd.department_index
			   and dp.personnel_sn='$number'
			 order by dp.personnel_index";
		$row=$DBsql->fetch_one_array($sql);
		if(isset($row['PERSONNEL_INDEX'])){
			echo '警号已经存在，请更换！';
		}else{
			echo 'y';
		}
	}else if($act=='add'){//增加新警员
		$qiye=$_POST['qiye'];//警号
		$chuche=$_POST['chuche'];//姓名
		$chepai=$_POST['chepai'];//警号
		$mudi=$_POST['mudi'];//派出所
		$shiyou=$_POST['shiyou']; //手持设备编号
		$shenqingren=$_POST['shenqingren']; 
		$time="to_date('".date('Y-m-d')."', 'yyyy-mm-dd hh24:mi:ss')";
		$guihuan="to_date('".$_POST['guihuan']."', 'yyyy-mm-dd hh24:mi:ss')";
		$sql="insert into gps_car_use_approval
				  (ID,EN_ID,
				   OUT_CAR_NUMB,
				   CAR_ID,
				   ADDRESS,
				   SUBJECT_MATTER,
				   USER_ID,
				   SQ_STATUS,
				   PILOTID,
				   SQ_TIME,
				   SY_TIME,
				   GH_TIME)
				values
				  (GPS_CAR_USE_APPROVAL_SEQ.nextval,
				  '$qiye',
				  '$chuche','$chepai','$mudi','$shiyou','$shenqingren',0,2,".$time.",".$time.",".$guihuan.")";
	if($DBsql->query($sql)){
			echo '{
				"info":"数据更新成功！",
				"status":"y"
			}';
		}else{
			echo '{
				"info":"数据更新失败！",
				"status":"n"
			}';
		}
	}else if($act=='del'){
		if(substr($act,0,3)=='del'){
		$sn=$_POST['sn'];
		$sql="delete from "."gps_car_use_approval"." dp where dp.id in ($sn)";
		$DBsql->query($sql);
		echo 'y';
		}
	}
}
?>